﻿using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using VioletDevelopmentLaboratory.Net.Web.Component;

namespace VioletDevelopmentLaboratory.Net.Web
{
	public class MasterPageBase : MasterPage
	{
		protected override void OnInit(EventArgs e)
		{
			base.OnInit(e);

			if (this.Page.Header.FindControl("VWebGlobalStyleSheet") == null)
			{
				Literal styleLiteral = new Literal();
				styleLiteral.ID = "VWebGlobalStyleSheet";
				styleLiteral.Text = "<!--VBoard//--><link rel=\"stylesheet\" type=\"text/css\" href=\"" + VUtility.GetGlobalStyleSheetUrl() + "\" />\r\n";

				this.Page.Header.Controls.Add(styleLiteral);
			}

			ClientScriptManager clientScript = this.Page.ClientScript;

			if (!clientScript.IsClientScriptIncludeRegistered("VWebGlobalJavaScript"))
			{
				clientScript.RegisterClientScriptInclude("VWebGlobalJavaScript", VUtility.GetGlobalJavaScriptUrl());
			}

			if (!clientScript.IsClientScriptBlockRegistered("VWebGlobalMessagePanel_HTML"))
			{
				clientScript.RegisterClientScriptBlock(this.Page.GetType(), "VWebGlobalMessagePanel_HTML", "<!--VBoard//--><div id=\"VWebPlaceHolder\"></div><script type=\"text/javascript\">VWebGlobal_SetMessagePanel();</script>");
			}

			if (!clientScript.IsClientScriptBlockRegistered("VWebGlobalCurrentScroll"))
			{
				clientScript.RegisterClientScriptBlock(this.Page.GetType(), "VWebGlobalCurrentScroll", "<!--VBoard//--><input type=\"hidden\" id=\"VWebCurrentScrollX\" name=\"VWebCurrentScrollX\" /><input type=\"hidden\" id=\"VWebCurrentScrollY\" name=\"VWebCurrentScrollY\" /><script type=\"text/javascript\">setInterval('VWebGlobal_SetCurrentScroll();', 500);</script>");
			}
		}

		protected override void OnPreRender(EventArgs e)
		{
			base.OnPreRender(e);

			String x = "0";
			String y = "0";

			if (Page.IsPostBack)
			{
				x = String.IsNullOrEmpty(Request.Form["VWebCurrentScrollX"]) ? "0" : Request.Form["VWebCurrentScrollX"];
				y = String.IsNullOrEmpty(Request.Form["VWebCurrentScrollY"]) ? "0" : Request.Form["VWebCurrentScrollY"];
			}
			else
			{
				x = String.IsNullOrEmpty(Request.QueryString["x"]) ? "0" : Request.QueryString["x"];
				y = String.IsNullOrEmpty(Request.QueryString["y"]) ? "0" : Request.QueryString["y"];
			}

			ClientScriptManager clientScript = this.Page.ClientScript;

			if (!clientScript.IsStartupScriptRegistered("VWebGlobalScrollTo"))
			{
				clientScript.RegisterStartupScript(this.Page.GetType(), "VWebGlobalScrollTo", String.Format("window.scrollTo({0}, {1});", x, y), true);
			}
		}
	}

	public class PageBase : Page
	{
		protected override void OnInit(EventArgs e)
		{
			base.OnInit(e);

			if (this.Page.Header.FindControl("VWebGlobalStyleSheet") == null)
			{
				Literal styleLiteral = new Literal();
				styleLiteral.ID = "VWebGlobalStyleSheet";
				styleLiteral.Text = "<!--VBoard//--><link rel=\"stylesheet\" type=\"text/css\" href=\"" + VUtility.GetGlobalStyleSheetUrl() + "\" />\r\n";

				this.Page.Header.Controls.Add(styleLiteral);
			}

			ClientScriptManager clientScript = this.Page.ClientScript;

			if (!clientScript.IsClientScriptIncludeRegistered("VWebGlobalJavaScript"))
			{
				clientScript.RegisterClientScriptInclude("VWebGlobalJavaScript", VUtility.GetGlobalJavaScriptUrl());
			}

			if (!clientScript.IsClientScriptBlockRegistered("VWebGlobalMessagePanel_HTML"))
			{
				clientScript.RegisterClientScriptBlock(this.Page.GetType(), "VWebGlobalMessagePanel_HTML", "<!--VBoard//--><div id=\"VWebPlaceHolder\"></div><script type=\"text/javascript\">VWebGlobal_SetMessagePanel();</script>");
			}

			if (!clientScript.IsClientScriptBlockRegistered("VWebGlobalCurrentScroll"))
			{
				clientScript.RegisterClientScriptBlock(this.Page.GetType(), "VWebGlobalCurrentScroll", "<!--VBoard//--><input type=\"hidden\" id=\"VWebCurrentScrollX\" name=\"VWebCurrentScrollX\" /><input type=\"hidden\" id=\"VWebCurrentScrollY\" name=\"VWebCurrentScrollY\" /><script type=\"text/javascript\">setInterval('VWebGlobal_SetCurrentScroll();', 500);</script>");
			}
		}

		protected override void OnPreRender(EventArgs e)
		{
			base.OnPreRender(e);

			String x = "0";
			String y = "0";

			if (Page.IsPostBack)
			{
				x = String.IsNullOrEmpty(Request.Form["VWebCurrentScrollX"]) ? "0" : Request.Form["VWebCurrentScrollX"];
				y = String.IsNullOrEmpty(Request.Form["VWebCurrentScrollY"]) ? "0" : Request.Form["VWebCurrentScrollY"];
			}
			else
			{
				x = String.IsNullOrEmpty(Request.QueryString["x"]) ? "0" : Request.QueryString["x"];
				y = String.IsNullOrEmpty(Request.QueryString["y"]) ? "0" : Request.QueryString["y"];
			}

			ClientScriptManager clientScript = this.Page.ClientScript;

			if (!clientScript.IsStartupScriptRegistered("VWebGlobalScrollTo"))
			{
				clientScript.RegisterStartupScript(this.Page.GetType(), "VWebGlobalScrollTo", String.Format("window.scrollTo({0}, {1});", x, y), true);
			}
		}
	}

	public class UserControlBase : UserControl
	{
		protected override void OnInit(EventArgs e)
		{
			base.OnInit(e);

			if (this.Page.Header.FindControl("VWebGlobalStyleSheet") == null)
			{
				Literal styleLiteral = new Literal();
				styleLiteral.ID = "VWebGlobalStyleSheet";
				styleLiteral.Text = "<!--VBoard//--><link rel=\"stylesheet\" type=\"text/css\" href=\"" + VUtility.GetGlobalStyleSheetUrl() + "\" />\r\n";

				this.Page.Header.Controls.Add(styleLiteral);
			}

			ClientScriptManager clientScript = this.Page.ClientScript;

			if (!clientScript.IsClientScriptIncludeRegistered("VWebGlobalJavaScript"))
			{
				clientScript.RegisterClientScriptInclude("VWebGlobalJavaScript", VUtility.GetGlobalJavaScriptUrl());
			}

			if (!clientScript.IsClientScriptBlockRegistered("VWebGlobalMessagePanel_HTML"))
			{
				clientScript.RegisterClientScriptBlock(this.Page.GetType(), "VWebGlobalMessagePanel_HTML", "<!--VBoard//--><div id=\"VWebPlaceHolder\"></div><script type=\"text/javascript\">VWebGlobal_SetMessagePanel();</script>");
			}

			if (!clientScript.IsClientScriptBlockRegistered("VWebGlobalCurrentScroll"))
			{
				clientScript.RegisterClientScriptBlock(this.Page.GetType(), "VWebGlobalCurrentScroll", "<!--VBoard//--><input type=\"hidden\" id=\"VWebCurrentScrollX\" name=\"VWebCurrentScrollX\" /><input type=\"hidden\" id=\"VWebCurrentScrollY\" name=\"VWebCurrentScrollY\" /><script type=\"text/javascript\">setInterval('VWebGlobal_SetCurrentScroll();', 500);</script>");
			}
		}

		protected override void OnPreRender(EventArgs e)
		{
			base.OnPreRender(e);

			String x = "0";
			String y = "0";

			if (Page.IsPostBack)
			{
				x = String.IsNullOrEmpty(Request.Form["VWebCurrentScrollX"]) ? "0" : Request.Form["VWebCurrentScrollX"];
				y = String.IsNullOrEmpty(Request.Form["VWebCurrentScrollY"]) ? "0" : Request.Form["VWebCurrentScrollY"];
			}
			else
			{
				x = String.IsNullOrEmpty(Request.QueryString["x"]) ? "0" : Request.QueryString["x"];
				y = String.IsNullOrEmpty(Request.QueryString["y"]) ? "0" : Request.QueryString["y"];
			}

			ClientScriptManager clientScript = this.Page.ClientScript;

			if (!clientScript.IsStartupScriptRegistered("VWebGlobalScrollTo"))
			{
				clientScript.RegisterStartupScript(this.Page.GetType(), "VWebGlobalScrollTo", String.Format("window.scrollTo({0}, {1});", x, y), true);
			}
		}
	}



	//public class MasterPageBase : MasterPage
	//{
	//    public Boolean IsValidPostBack
	//    {
	//        get
	//        {
	//            Int32 lastTicket = (Int32)Session["!VWeb_LastTicket"];

	//            Int32 thisTicket = Int32.Parse(Request["!VWeb_CurrentTicket"]);

	//            if (thisTicket > lastTicket)
	//            {
	//                Session["!VWeb_LastTicket"] = thisTicket;

	//                return true;
	//            }
	//            else
	//            {
	//                return false;
	//            }
	//        }
	//    }

	//    protected override void OnInit(EventArgs e)
	//    {
	//        base.OnInit(e);

	//        if (this.Page.Header.FindControl("VWebGlobalStyleSheet") == null)
	//        {
	//            Literal styleLiteral = new Literal();
	//            styleLiteral.ID = "VWebGlobalStyleSheet";
	//            styleLiteral.Text = "<link rel=\"stylesheet\" type=\"text/css\" href=\"" + VUtility.GetGlobalStyleSheetUrl() + "\" />\r\n";

	//            this.Page.Header.Controls.Add(styleLiteral);
	//        }

	//        if (this.Page.Header.FindControl("VWebGlobalJavaScript") == null)
	//        {
	//            Literal scriptLiteral = new Literal();
	//            scriptLiteral.ID = "VWebGlobalJavaScript";
	//            scriptLiteral.Text = "<script type=\"text/javascript\" src=\"" + VUtility.GetGlobalJavaScriptUrl() + "\"></script>\r\n";

	//            this.Page.Header.Controls.Add(scriptLiteral);
	//        }

	//        ClientScriptManager clientScript = this.Page.ClientScript;

	//        if (!clientScript.IsStartupScriptRegistered("VWebGlobalMessagePanel_HTML"))
	//        {
	//            clientScript.RegisterClientScriptBlock(this.Page.GetType(), "VWebGlobalMessagePanel_HTML", "<div id=\"VWebPlaceHolder\"></div><script type=\"text/javascript\">VWebGlobal_SetMessagePanel();</script>");
	//        }


	//        if (Session["!VWeb_LastTicket"] == null)
	//            Session["!VWeb_LastTicket"] = 0;

	//        if (Session["!VWeb_CurrentTicket"] == null)
	//            Session["!VWeb_CurrentTicket"] = 0;

	//        Int32 ticket = (Int32)Session["!VWeb_CurrentTicket"] + 1;

	//        Session["!VWeb_CurrentTicket"] = ticket;
	//    }

	//    protected override void OnPreRender(EventArgs e)
	//    {
	//        base.OnPreRender(e);

	//        Int32 ticket = (Int32)Session["!VWeb_CurrentTicket"] + 1;

	//        this.Page.ClientScript.RegisterHiddenField("!VWeb_CurrentTicket", ticket.ToString());
	//    }
	//}
}
